﻿using DapperExtensions;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
using WMLogistics.Model;
using WMLogistics.Model.Response;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Dapper;

namespace WMLogistics_Dao
{
   public class NoticeDao
    {
        private SqlGeneratorImpl sqlGenerator;
        public NoticeDao()
        {
            var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
            sqlGenerator = new SqlGeneratorImpl(config);
            DapperExtensions.DapperExtensions.Configure(config);
        }

        public wuliu_notice Get(int id)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Get<wuliu_notice>(id);
            }
        }
        public wuliu_wlcompany_notice GetWlNotice(int id)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Get<wuliu_wlcompany_notice>(id);
            }
        }
       

        public PageData<wuliu_wlcompany_notice> GetPageList(IPredicate predicate, IList<ISort> sorts, int pageIndex, int pageSize)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                var pageData = new PageData<wuliu_wlcompany_notice>();
                pageData.Items = conn.GetPage<wuliu_wlcompany_notice>(predicate, sorts, pageIndex, pageSize);
                pageData.TotalNum = conn.Count<wuliu_wlcompany_notice>(predicate);
                pageData.CurrentPage = pageIndex;
                pageData.TotalPageCount = pageData.TotalNum % pageSize == 0 ? pageData.TotalNum / pageSize : pageData.TotalNum / pageSize + 1;
                return pageData;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteNotice(wuliu_wlcompany_notice model)
        {

            using (var conn = ConnectionHelper.OpenConnection())
				 {
					StringBuilder str=new StringBuilder();
					StringBuilder strSql=new StringBuilder();
					strSql.Append("update wuliu_wlcompany_notice set ");
					if(model.wuliu_no.IfNotNull())
						strSql.Append("wuliu_no=@wuliu_no,");
					if(model.notice_id.IfNotNull())
						strSql.Append("notice_id=@notice_id,");
					if(model.title.IfNotNull())
						strSql.Append("title=@title,");
					if(model.release_time.IfNotNull())
						strSql.Append("release_time=@release_time,");
					if(model.iread.IfNotNull())
						strSql.Append("iread=@iread,");
					if(model.read_time.IfNotNull())
						strSql.Append("read_time=@read_time,");
					if(model.notice_source.IfNotNull())
						strSql.Append("notice_source=@notice_source,");
					if(model.idelete.IfNotNull())
						strSql.Append("idelete=@idelete,");
					if(model.delete_time.IfNotNull())
						strSql.Append("delete_time=@delete_time,");
					if(model.Operator.IfNotNull())
						strSql.Append("operator=@operator,");
					if(model.operator_time.IfNotNull())
						strSql.Append("operator_time=@operator_time,");
					if(model.notice_type.IfNotNull())
						strSql.Append("notice_type=@notice_type");
					str.Append(strSql.ToString().TrimEnd(','));
					str.Append(" where id=@id ");
					return conn.Execute(str.ToString(), model)>0;
				}

        }

        public int Add(wuliu_notice notice)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Insert<wuliu_notice>(notice);
            }
        }
    }
}
